package cn.plushine.controller;


import cn.plushine.pojo.Unit;
import cn.plushine.service.BaseUnitService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags = "单元信息模块")
@RestController
@RequestMapping("/admin/product/unit")
public class BaseUnitController {

    @Autowired
    BaseUnitService baseUnitService;

    // 获取单位信息的列表
    @ApiOperation(value = "获取单位信息的列表")
    @GetMapping("/get")
    public HashMap<String, Object> get(String input) {
        try {
            // 获取单元信息列表
            List<Unit> unitDB = baseUnitService.get(input);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取单元信息成功！", unitDB);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "获取单元信息失败！", null);
        }
    }

    // 删除单位信息
    @ApiOperation(value = "删除单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", example = "1",value = "单位id", required = true, paramType = "path", dataType = "int")
    })
    @DeleteMapping("/delete/{id}")
    public HashMap<String, Object> delete(@PathVariable int id) {
        try {
            // 删除单位信息
            baseUnitService.delete(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "删除单位信息成功！", null);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "删除单位信息失败！", null);
        }
    }

    // 修改单位信息
    @ApiOperation(value = "修改单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unit", value = "单位信息", required = true, paramType = "body", dataType = "Unit")
    })
    @PostMapping("/save")
    public HashMap<String, Object> save(@RequestBody Unit unit) {
        try {
            // 修改单位信息
            baseUnitService.save(unit);
            // 成功响应
            return ResMapUtils.encapsulation(200, "修改单位信息成功！", null);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "修改单位信息失败！", null);
        }
    }

    // 添加单位信息
    @ApiOperation(value = "添加单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unit", value = "单位信息", required = true, paramType = "body", dataType = "Unit")
    })
    @PostMapping("/add")
    public HashMap<String, Object> add(@RequestBody Unit unit) {
        try {
            // 添加单位信息
            baseUnitService.add(unit);
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加单位信息成功！", null);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "添加单位信息失败！", null);
        }
    }
}
